package com.fzz.learning_forum.controller;

import com.fzz.learning_forum.domain.Course;
import com.fzz.learning_forum.domain.Course_People;
import com.fzz.learning_forum.domain.User;
import com.fzz.learning_forum.service.CourseService;
import com.fzz.learning_forum.serviceImpl.CourseServiceImpl;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class CourseController {

    @Autowired
    private CourseService courseService=new CourseServiceImpl();

    //创建课程
    @RequestMapping(value = "/teacher/createdCourse")
    @ResponseBody
    public String createdCourse(Course course)
    {
        System.out.println(course);
        try {
            Integer rows = courseService.createCourse(course);
            if(rows>0)
            {
                return "OK";
            }
        }
        catch (Exception e)
        {
            System.out.println(e.getCause());
        }
        return "NO";
    }

    //课程详细信息
    @RequestMapping(value = "/user/courseDetails/{id}")
    public String courseDetail(HttpSession session,HttpServletRequest request,@PathVariable(value = "id")Integer id)
    {
        Course course = courseService.selectCourseDetails(id);
        session.setAttribute("course",course);
        getCoursePeopleWithPage(request,id);
        return "client/courseDetails";
    }
    // 查询该课程的所有用户
    private void getCoursePeopleWithPage(HttpServletRequest request,Integer course_id) {
        // upage表示用户页数，coursePage
        String cpage = request.getParameter("cpage");
        cpage = StringUtils.isBlank(cpage) ? "1" : cpage;
        request.setAttribute("course", cpage);
        PageInfo<Course_People> course_peopleList = courseService.selectAllCoursePeolpeById(course_id,Integer.parseInt(cpage), 5);
        request.setAttribute("course", cpage);
        request.setAttribute("course_peopleList", course_peopleList);
        System.out.println(course_peopleList);
    }
}
